package com.xiguthings.waka.service.userCDcard.impl;

import com.xiguthings.waka.dao.DataBaseStatus;
import com.xiguthings.waka.dao.mapper.CoolDownCardMapper;
import com.xiguthings.waka.dao.mapper.UserCoolDownCardMapper;
import com.xiguthings.waka.dao.mapper.UserMapper;
import com.xiguthings.waka.exception.BussinessException;
import com.xiguthings.waka.po.*;
import com.xiguthings.waka.po.extpo.Page;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.service.userCDcard.IUserCDcardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserCDcardServiceImpl implements IUserCDcardService {

    @Resource
    private UserCoolDownCardMapper userCoolDownCardMapper;
    @Resource
    private CoolDownCardMapper coolDownCardMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int countByExample(UserCoolDownCardExample userCoolDownCardExample) throws Exception {
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int deleteByExample(UserCoolDownCardExample userCoolDownCardExample) throws Exception {
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int deleteByPrimaryKey(String id) throws Exception {
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int insert(UserCoolDownCard userCoolDownCard) throws Exception {
        // 查询数据库中可以获得的加速卡
        CoolDownCardExample cdCardExample = new CoolDownCardExample();
        CoolDownCardExample.Criteria cdCardCriteria = cdCardExample.createCriteria();
        cdCardCriteria.andStatusNotEqualTo(DataBaseStatus.DELETE);// status <> 99
        List<CoolDownCard> cdCardList = coolDownCardMapper.selectByExample(cdCardExample);
        CoolDownCard coolDownCard = null;
        if (cdCardList != null && cdCardList.size() > 0) {
            coolDownCard = cdCardList.get(0);
        }
        if (coolDownCard == null) {
            return 0;
        }

        // 如果查询到了可以获得的加速卡
        userCoolDownCard.setCoolDownCardId(coolDownCard.getCoolDownCardId());// 加入关联
        // 设置id
        String id = StringUtils.getRandomUUID();
        userCoolDownCard.setUserCoolDownCardId(id);
        // 设置时间
        userCoolDownCard.setCreateTime(new Date());
        // 设置状态为默认
        userCoolDownCard.setStatus(DataBaseStatus.NORMAL);
        // 插入数据库
        return userCoolDownCardMapper.insert(userCoolDownCard);

    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int insertSelective(UserCoolDownCard userCoolDownCard) throws Exception {
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<UserCoolDownCard> selectByExample(UserCoolDownCardExample userCoolDownCardExample) throws Exception {
        List<UserCoolDownCard> list = userCoolDownCardMapper.selectByExample(userCoolDownCardExample);
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public UserCoolDownCard selectByPrimaryKey(String id) throws Exception {
        return null;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByExampleSelective(UserCoolDownCard userCoolDownCard, UserCoolDownCardExample userCoolDownCardExample) throws Exception {
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByExample(UserCoolDownCard userCoolDownCard, UserCoolDownCardExample userCoolDownCardExample) throws Exception {
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(UserCoolDownCard userCoolDownCard) throws Exception {
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByPrimaryKey(UserCoolDownCard userCoolDownCard) throws Exception {
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page selectByPage(Page pageEntity, UserCoolDownCardExample userCoolDownCardExample) throws Exception {
        return null;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public boolean useCDcardByPrimaryKey(String id) throws Exception {
        // 查询数据库 用户拥有的加速卡
        UserCoolDownCard userCoolDownCardDB = userCoolDownCardMapper.selectByPrimaryKey(id);
        if (userCoolDownCardDB == null) {
            BussinessException.throwThis(CODE_FAULT, "失败，没有这个加速卡");
        }
        // 查询加速卡
        CoolDownCard coolDownCard = coolDownCardMapper.selectByPrimaryKey(userCoolDownCardDB.getCoolDownCardId());
        // 查询用户
        User user = userMapper.selectByPrimaryKey(userCoolDownCardDB.getUserId());
        // 修改用户的开始冷却时间
        Date cdBeginTime = user.getCdBeginTime();
        long time = 0L;
        if (cdBeginTime != null) {
            time = cdBeginTime.getTime();
        }

        // 用户的 - 加速卡的 = 已经加速的时间
        time = time - coolDownCard.getHotValue();
        user.setCdBeginTime(new Date(time));// 修改用户
        userCoolDownCardDB.setStatus(DataBaseStatus.DISABLED);// 修改用户的加速卡
        // 修改数据库
        userMapper.updateByPrimaryKey(user);
        userCoolDownCardMapper.updateByPrimaryKey(userCoolDownCardDB);
        return true;
    }
}
